<template>
  <div>
  <el-dialog
    :title="!dataForm.id ? $t('handle.add') :  btnshow ? '修改':'详情'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <div v-loading = loading>
    <el-form :model="dataForm" :rules="dataRule" :disabled="formDisabled" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="130px">
    <el-form-item label="区域名称" prop="regionId">
      <el-input v-model="this.regionAllName" placeholder="区域名称" :disabled=true></el-input>
    </el-form-item>
    <el-form-item label="地址名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="地址名称"></el-input>
    </el-form-item>
    <el-row>
        <el-col :span="12">
          <el-form-item label="允许落户判断标识" prop="allowedToSettle" >
                  <el-select style="width:100%;" v-model="dataForm.allowedToSettle" placeholder="请选择允许落户判断标识">
                    <el-option
                      v-for="item in allowedToSettleList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="适合居住判断标识" prop="goodLive">
                  <el-select style="width:100%;" v-model="dataForm.goodLive" placeholder="请选择适合居住判断标识">
                    <el-option
                      v-for="item in goodLiveList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
          </el-form-item>
        </el-col>
    </el-row>
    <el-row>
        <el-col :span="12">
          <el-form-item label="地址存在标识" prop="addressPresence">
                  <el-select style="width:100%;" v-model="dataForm.addressPresence" placeholder="请选择地址存在标识">
                    <el-option
                      v-for="item in addressPresenceList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址在用标识" prop="addressIdentified">
                  <el-select style="width:100%;" v-model="dataForm.addressIdentified" placeholder="请选择地址在用标识">
                    <el-option
                      v-for="item in addressIdentifiedList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
          </el-form-item>
        </el-col>
    </el-row>
    <el-row>
        <el-col :span="12">
          <el-form-item label="撤销时间" prop="cancellationTime">
                 <el-date-picker style="width:100%;"
                  v-model="dataForm.cancellationTime"
                  type="date"
                  placeholder="选择撤销时间"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新时间" prop="updateTime">
                 <el-date-picker style="width:100%;"
                  v-model="dataForm.updateTime"
                  type="date"
                  placeholder="选择更新时间"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
          </el-form-item>
        </el-col>
    </el-row>
    <el-row>
        <el-col :span="12">
          <el-form-item label="启用日期" prop="openingDate">
                 <el-date-picker style="width:100%;"
                  v-model="dataForm.openingDate"
                  type="date"
                  placeholder="选择启用日期"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="停用日期" prop="deactivationDate">
                 <el-date-picker style="width:100%;"
                  v-model="dataForm.deactivationDate"
                  type="date"
                  placeholder="选择停用日期"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
          </el-form-item>
        </el-col>
    </el-row>
    <el-row>
        <el-col :span="12">
          <el-form-item label="警务责任区代码" prop="policeDistrictCode">
            <el-input v-model="dataForm.policeDistrictCode" placeholder="警务责任区代码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="登记单位" prop="applicantName">
            <el-input :disabled="true" v-model="dataForm.applicantName" placeholder="登记单位" style="width:79%"></el-input><el-button style="width:20%" type="info" plain :disabled=false  @click="selectUnit()">选择</el-button>
          </el-form-item>
        </el-col>
    </el-row>
    <el-row>
        <el-col :span="12">
          <el-form-item label="登记人姓名" prop="registrantName">
            <el-input :disabled="true" v-model="dataForm.registrantName" placeholder="登记人姓名" style="width:79%"></el-input><el-button style="width:20%" type="info" plain :disabled=false  @click="selectPopulation()">选择</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="登记时间" prop="registrationTime">
                 <el-date-picker style="width:100%;"
                  v-model="dataForm.registrationTime"
                  type="date"
                  placeholder="选择登记时间"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
          </el-form-item>
        </el-col>
    </el-row>
    <el-row>
        <el-col :span="12">
          <el-form-item label="坐标经度" prop="longitude">
            <el-input v-model="dataForm.longitude" :disabled="true" placeholder="坐标经度"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="坐标纬度" prop="latitude">
            <el-input v-model="dataForm.latitude" :disabled="true" placeholder="坐标纬度"></el-input>
          </el-form-item>
        </el-col>
    </el-row>
     <gdMap :dataForm="dataForm" ref="gdMap" :regionAllName="regionAllName"></gdMap>
    </el-form>
    <div style="text-align:right;margin-top: 70px;">
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('handle.cancel') }}</el-button>
      <el-button type="primary" v-show="btnshow" @click="dataFormSubmit()">{{ $t('handle.confirm') }}</el-button>
    </span>
    </div>
  </div>
  </el-dialog>
    <atual-unit-dialog v-if="selectUnitVisible" ref="atualUnitDialog" @receiveParameters="getUnitParameters"></atual-unit-dialog>
    <atual-population-dialog v-if="selectPopulationVisible" ref="atualPopulationDialog" @receiveParametersPeople="getPopulationParameters"></atual-population-dialog>
  </div>
</template>

<script>
  import { getDictList,checkAddress} from '@/utils'
  import AtualUnitDialog from "../dialog/actuaunitDialog";
  import AtualPopulationDialog from "../dialog/actualPopulationDialog";
  import gdMap from "../dialog/gdMap"
  export default {
    created() {
        getDictList('allowed_to_settle').then(({data}) => {
          this.allowedToSettleList = data.dictList;
        })
        getDictList('good_live').then(({data}) => {
          this.goodLiveList = data.dictList;
        })
        getDictList('address_presence').then(({data}) => {
          this.addressPresenceList = data.dictList;
        })
        getDictList('address_identified').then(({data}) => {
          this.addressIdentifiedList = data.dictList;
        })
    },
    data () {
      let checkAddressName = (rule, value, callback) => {
        if(!value){
          return callback(new Error("地址名称不能为空"));
        }else if(value){
          checkAddress({'id':this.dataForm.id,'name':this.dataForm.name,'regionId':this.regionId}).then(({data}) => {
                if (data && data.code === 0 && parseInt(data.data) > 0) {
                  return callback(new Error("地址名称已存在"));
                }else{
                  return callback();
                }
            })
        }else{
        }
      };
      return {
        loading:false,
        visible: false,
        selectUnitVisible: false,
        selectPopulationVisible: false,
        allowedToSettleList: '',
        goodLiveList: '',
        addressPresenceList: '',
        addressIdentifiedList: '',
        btnshow: true,
        formDisabled: false,
        dataForm: {
          id: '',
          regionId: '',
          name: '',
          policeDistrictCode: '',
          allowedToSettle: '',
          goodLive: '',
          addressPresence: '',
          addressIdentified: '',
          cancellationTime: '',
          updateTime: '',
          regionAllName: this.regionAllName,
          openingDate: '',
          deactivationDate: '',
          applicant: '',
          applicantName: '',
          registrant: '',
          registrantName: '',
          registrationTime: '',
          longitude: '',
          latitude: ''
        },
        dataRule: {
          name: [
            { validator: checkAddressName, required: true, trigger: "blur" }
          ],
          policeDistrictCode: [
            { required: true, message: '警务责任区代码不能为空', trigger: 'blur' }
          ],
          applicantName: [
            { required: true, message: '登记单位不能为空', trigger: 'blur' }
          ],
          registrantName: [
            { required: true, message: '登记人姓名不能为空', trigger: 'blur' }
          ]
          // registrationTime: [
          //   { required: true, message: '登记时间不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    components: {
      AtualUnitDialog,
      AtualPopulationDialog,
      "gdMap":gdMap
    },
    computed: {
      regionId() {
        return this.$store.state.address.regionId;
      },
      regionAllName() {
        return (this.$store.state.address.regionPnames ? this.$store.state.address.regionPnames + ',' : '') +this.$store.state.address.regionName;
      }
    },
    methods: {
      selectUnit() {
        this.selectUnitVisible = true;
        this.$nextTick(() => {
          this.$refs.atualUnitDialog.init();
        });
      },
      getUnitParameters(data){
        this.dataForm.applicant = data.selectId;
        this.dataForm.applicantName = data.selectText;
      },
      selectPopulation() {
        this.selectPopulationVisible = true;
        this.$nextTick(() => {
          this.$refs.atualPopulationDialog.init();
        });
      },
      getPopulationParameters(data){
        this.dataForm.registrant = data.selectId;
        this.dataForm.registrantName = data.selectText;
      },
      init (id,detail) {
        this.dataForm.id = id || ''
        this.visible = true
        this.btnshow = true
        this.formDisabled = false
        if (detail) {
          this.btnshow = false
          this.formDisabled = true
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/dg-standard-info/dgstandardaddress/info/${id}`,
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.dgStandardAddress.name
                this.dataForm.policeDistrictCode = data.dgStandardAddress.policeDistrictCode
                this.dataForm.allowedToSettle = data.dgStandardAddress.allowedToSettle
                this.dataForm.goodLive = data.dgStandardAddress.goodLive
                this.dataForm.addressPresence = data.dgStandardAddress.addressPresence
                this.dataForm.addressIdentified = data.dgStandardAddress.addressIdentified
                this.dataForm.cancellationTime = data.dgStandardAddress.cancellationTime
                this.dataForm.updateTime = data.dgStandardAddress.updateTime
                this.dataForm.openingDate = data.dgStandardAddress.openingDate
                this.dataForm.deactivationDate = data.dgStandardAddress.deactivationDate
                if(data.dgStandardAddress.applicant == null){
                  this.dataForm.applicant = ''
                  this.dataForm.applicantName = ''
                }else{
                  this.dataForm.applicant = data.dgStandardAddress.applicant.unitCode
                  this.dataForm.applicantName = data.dgStandardAddress.applicant.name
                }
                 if(data.dgStandardAddress.registrant == null){
                  this.dataForm.registrant = ''
                  this.dataForm.registrantName = ''
                }else{
                  this.dataForm.registrant = data.dgStandardAddress.registrant.populationCoding
                  this.dataForm.registrantName = data.dgStandardAddress.registrant.name
                }

                this.dataForm.registrationTime = data.dgStandardAddress.registrationTime
                this.dataForm.longitude = data.dgStandardAddress.longitude
                this.dataForm.latitude = data.dgStandardAddress.latitude
                this.$refs.gdMap.initSearch(this.dataForm.longitude,this.dataForm.latitude)
              }
            })
          }else{
            this.$refs.gdMap.initSearch(null,null)
          }
        })
        // this.$nextTick(() => {
        //   console.log()

        // })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: '/dg-standard-info/dgstandardaddress/addorupdateaddress',
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'regionId': this.regionId,
                'name': this.dataForm.name,
                'policeDistrictCode': this.dataForm.policeDistrictCode,
                'allowedToSettle': this.dataForm.allowedToSettle,
                'goodLive': this.dataForm.goodLive,
                'addressPresence': this.dataForm.addressPresence,
                'addressIdentified': this.dataForm.addressIdentified,
                'cancellationTime': this.dataForm.cancellationTime,
                'updateTime': this.dataForm.updateTime,
                'openingDate': this.dataForm.openingDate,
                'deactivationDate': this.dataForm.deactivationDate,
                'applicant': this.dataForm.applicant,
                'registrant': this.dataForm.registrant,
                'registrationTime': this.dataForm.registrationTime,
                'longitude': this.dataForm.longitude,
                'latitude': this.dataForm.latitude
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.loading = false
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.loading = false
              }
            })
          }
        })
      }
    }
  }
</script>
